package com.caiwangdai.app.ui.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.caiwangdai.app.BuildConfig
import com.caiwangdai.app.R
import com.caiwangdai.app.backend.RetrofitHome
import com.caiwangdai.app.backend.observer.OnDataV2Listener
import com.caiwangdai.app.backend.rx.RxSchedulers
import com.caiwangdai.app.bean.UserB
import com.caiwangdai.app.bean.base.BaseB
import com.caiwangdai.app.bean.base.NormalResponseB
import com.caiwangdai.app.extension.*
import com.caiwangdai.app.origin.KeyBoardObserverActivity
import com.caiwangdai.app.origin.ToolBarManager
import com.caiwangdai.app.origin.web.NormalWebActivity
import com.caiwangdai.app.utils.AuthUtils
import com.caiwangdai.app.widget.SMSCountDownTimer
import kotlinx.android.synthetic.main.activity_login.*

//登录界面
class LoginActivity : KeyBoardObserverActivity() {
    override fun getLayoutResID() = R.layout.activity_login

    private var mobile: String = ""
    private var verifyCode: String = ""
    private lateinit var smsCounter: SMSCountDownTimer

    companion object {
        fun newInstance(context: Context): Intent {
            val intent = Intent(context, LoginActivity::class.java)
            return intent
        }
    }

    override fun initView() {
        //短信倒计时
        smsCounter = SMSCountDownTimer(tv_send_code)

        //Toolbar
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.white)
                .setNavigationIcon(R.drawable.ic_nav_close_circle)

        //send code
        tv_send_code.setOnClickListenerPro {
            if (checkMobile()) getCode()
        }

        //btn submit
        btn_login.setOnClickListenerPro {
            if (checkValue()) actionLogin()
        }

        //register
        btn_register.setOnClickListenerPro {
            startActivity(RegisterActivity.newInstance(mContext))
        }
    }


    //检查手机号码
    private fun checkMobile(): Boolean {
        mobile = et_mobile.getContent()
        if (mobile.isEmpty()) {
            toast("请输入手机号码")
            return false
        }
        if (!AuthUtils.isMobileNo(mobile)) {
            toast("请确保手机号格式输入正确")
            return false
        }
        return true
    }

    //检查所有值
    private fun checkValue(): Boolean {
        mobile = et_mobile.getContent()
        verifyCode = et_verify_code.getContent()
        if (!checkMobile()) return false

        if (!BuildConfig.DEBUG && !smsCounter.getCodeIng) {
            toast("请先获取验证码")
            return false
        }

        if (verifyCode.isEmpty()) {
            toast("请输入验证码")
            return false
        }
        return true
    }


    //获取验证码
    private fun getCode() {
        RetrofitHome
                .getUserApi()
                .getVerifyCodeInLogin(mobile)
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<BaseB>(this) {
                    override fun getProgressBarContent() = "获取验证码..."
                    override fun onSuccess(value: NormalResponseB<BaseB>) {
                        smsCounter.startCountTime()
                    }
                })
    }


    /**
     * 开始登录
     */
    private fun actionLogin() {
        RetrofitHome
                .getUserApi()
                .login(mobile, verifyCode)
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<UserB>(this) {
                    override fun getProgressBarContent() = "登录中..."
                    override fun onSuccess(value: NormalResponseB<UserB>) {
                        toast("登录成功")
                        logIn(value.data)
                        setResult(Activity.RESULT_OK)
                        closePage()
                    }

                })

    }

    //返回
    override fun onBackPressed() {
        setResult(RESULT_CANCELED)
        closePage()
    }

    //关闭页面
    private fun closePage() {
        smsCounter.endCountTime()
        finish()
        overridePendingTransition(0, R.anim.slide_out_from_bottom)
    }

    override fun onKeyBoardShow() {
        iv_app_logo.gone()
    }

    override fun onKeyBoardDismiss() {
        iv_app_logo.visible()
    }
}
